home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.02 Feb 95 / Yenta / Erics C++ Libraries / Periodic Task Classes / CPPPeriodicTask.cp next >
Encoding:
Text File  |  1996-04-04  |  3.4 KB  |  123 lines  |  [TEXT/KAHL]

  1. /********************************************************* DEFINITION
  2.     DATE:    9/16/93
  3.     AUTHOR: Eric R. Rosé
  4.     
  5.     CLASS:  CPPPeriodicTask
  6.     
  7.     SUPERCLASS: CPPObject
  8.     
  9.         This C++ class is an abstract class for objects which
  10.         perform some periodic action.
  11.     
  12. ********************************************************************/
  13.  
  14.  
  15. #include <CPPPeriodicTask.h>
  16. #include <CPPTaskManager.h>
  17.  
  18. /*-----------------------------------------------------------------*/
  19. /*------------------------ PUBLIC METHODS -------------------------*/
  20. /*-----------------------------------------------------------------*/
  21.  
  22.     CPPPeriodicTask::CPPPeriodicTask (CPPTaskManager *TaskManager, 
  23.                          long minPeriod,
  24.                             Boolean deleteWhenCompleted)
  25.     {
  26.         this->minimumPeriod = minPeriod;
  27.         this->lastCalled = -32767;    // so we will be called immediately
  28.         this->timesCalled = 0;    // just because we're interested
  29.         this->hasCompleted = TRUE;
  30.         this->deleteWhenDone = deleteWhenCompleted;
  31.         this->ourManager = TaskManager;
  32.         this->completion = NULL;
  33.     }
  34.  
  35. /*-----------------------------------------------------------------*/
  36.  
  37.     CPPPeriodicTask::~CPPPeriodicTask (void)
  38.     {
  39.         // if we are still busy, remove us from our manager's queue
  40.         if (this->ourManager && (!this->hasCompleted))
  41.           this->ourManager->RemovePeriodicTask(this, FALSE);
  42.     }
  43.                     
  44. /*-----------------------------------------------------------------*/
  45.  
  46.     char *CPPPeriodicTask::ClassName (void)
  47.     {
  48.         return "CPPPeriodicTask";
  49.     }
  50.  
  51. /*-----------------------------------------------------------------*/
  52.     
  53.     long CPPPeriodicTask::GetPeriod (void)
  54.     /* accessor function for the period variable */
  55.     {
  56.         return this->minimumPeriod;
  57.     }
  58.  
  59. /*-----------------------------------------------------------------*/
  60.     
  61.     long CPPPeriodicTask::GetTimesCalled (void)
  62.     /* accessor function for the period variable */
  63.     {
  64.         return this->timesCalled;
  65.     }
  66.  
  67. /*-----------------------------------------------------------------*/
  68.  
  69.     void CPPPeriodicTask::SetPeriod (long newPeriod)
  70.     /* setter function for the period variable */
  71.     {
  72.         this->minimumPeriod = newPeriod;
  73.         // force it to run immediately
  74.         this->lastCalled = -32767;
  75.     }
  76.  
  77. /*-----------------------------------------------------------------*/
  78.  
  79.     Boolean    CPPPeriodicTask::NeedsToRun (void)
  80.     /* returns TRUE if the minimum period for this task has passed */
  81.     {
  82.         if (TickCount() - this->lastCalled > this->minimumPeriod)
  83.           return TRUE;
  84.         else
  85.           return FALSE;
  86.     }
  87.     
  88. /*-----------------------------------------------------------------*/
  89.  
  90.     OSErr CPPPeriodicTask::TaskError (void)
  91.     /* Accessor function for the error code returned by the last op. */ 
  92.     {
  93.         return this->callResult;
  94.     }
  95.  
  96. /*-----------------------------------------------------------------*/
  97.  
  98.     void CPPPeriodicTask::SetCompletionProc (CompletionProc NewProc)
  99.     /* set a special routine to be run when the task completes */
  100.     {
  101.         this->completion = NewProc;
  102.     }
  103.     
  104. /*-----------------------------------------------------------------*/
  105.  
  106.     void CPPPeriodicTask::DoPeriodicAction (void)
  107.     /* Do bookkeeping; subclass should override w/ something useful */
  108.     {
  109.         this->lastCalled = TickCount();
  110.         this->timesCalled++;
  111.     }
  112.  
  113. /*-----------------------------------------------------------------*/
  114.  
  115.     void CPPPeriodicTask::DoCompletedAction (void)
  116.     /* execute the completion routine, if there is one */
  117.     {
  118.         if (this->completion)
  119.           (*this->completion)((CPPObject *)this);
  120.     }
  121.  
  122. /*-----------------------------------------------------------------*/
  123.